package com.coolwen.controller;

import com.coolwen.entity.Collect;
import com.coolwen.service.student.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * Created by Nieyu on 2016/11/24.
 */
@Controller
@RequestMapping("/collect")
public class CollectController {

    @Autowired
    StudentService studentService;

    @RequestMapping(value = "/collect_info", method = RequestMethod.GET)
    public String collectAllInfo(Model model, String grade, String department, String major,String semester,HttpServletRequest request) throws UnsupportedEncodingException {

//        ModelAndView modelAndView = new ModelAndView();
//        modelAndView.setViewName("");
        model.addAttribute("g", grade);
        model.addAttribute("d", department);
        model.addAttribute("m", major);

        if (grade != null) {
            String sGrade = request.getParameter("grade");

            int gra = Integer.valueOf(sGrade);
            int graOne = gra + 1;

            semester = String.valueOf(gra) + "-" + String.valueOf(graOne) + "学年";
        }

        if (department != null) {
            String subDepartment = request.getParameter("department");
            department = subDepartment.substring(0, 2);
        }

        model.addAttribute("pagers", studentService.collectBySemester(grade, department, major, semester));

        List<Collect> collects = studentService.collectBySemesterAll(grade, department, major, semester);

        model.addAttribute("collects", collects);

        return "page/collect";
    }

    @RequestMapping(value = "/secondSemester", method = RequestMethod.GET)
    public String collectSecond(Model model, String grade, String department, String major,String semester,HttpServletRequest request) throws UnsupportedEncodingException {

        model.addAttribute("g", grade);
        model.addAttribute("d", department);
        model.addAttribute("m", major);

        if (grade != null) {
            String sGrade = request.getParameter("grade");

            int gra = Integer.valueOf(sGrade);
            int graOne = gra + 1;
            int graTwo = gra + 2;

            semester = String.valueOf(graOne) + "-" + String.valueOf(graTwo) + "学年";
        }

        if (department != null) {
            String subDepartment = request.getParameter("department");
            department = subDepartment.substring(0, 2);
        }

        model.addAttribute("pagers", studentService.collectBySemester(grade, department, major, semester));

        List<Collect> collects = studentService.collectBySemesterAll(grade, department, major, semester);

        model.addAttribute("collects", collects);

        return "page/collectSecondSemester";
    }
    @RequestMapping(value = "/thirdSemester", method = RequestMethod.GET)
    public String collectThird(Model model, String grade, String department, String major,String semester,HttpServletRequest request) throws UnsupportedEncodingException {

        model.addAttribute("g", grade);
        model.addAttribute("d", department);
        model.addAttribute("m", major);

        if (grade != null) {
            String sGrade = request.getParameter("grade");

            int gra = Integer.valueOf(sGrade);
            int graOne = gra + 2;
            int graTwo = gra + 3;

            semester = String.valueOf(graOne) + "-" + String.valueOf(graTwo) + "学年";
        }

        if (department != null) {
            String subDepartment = request.getParameter("department");
            department = subDepartment.substring(0, 2);
        }

        model.addAttribute("pagers", studentService.collectBySemester(grade, department, major, semester));

        List<Collect> collects = studentService.collectBySemesterAll(grade, department, major, semester);

        model.addAttribute("collects", collects);

        return "page/collectThirdSemester";
    }
    @RequestMapping(value = "/fourthSemester", method = RequestMethod.GET)
    public String collectFourth(Model model, String grade, String department, String major,String semester,HttpServletRequest request) throws UnsupportedEncodingException {

        model.addAttribute("g", grade);
        model.addAttribute("d", department);
        model.addAttribute("m", major);

        if (grade != null) {
            String sGrade = request.getParameter("grade");

            int gra = Integer.valueOf(sGrade);
            int graOne = gra + 3;
            int graTwo = gra + 4;

            semester = String.valueOf(graOne) + "-" + String.valueOf(graTwo) + "学年";
        }

        if (department != null) {
            String subDepartment = request.getParameter("department");
            department = subDepartment.substring(0, 2);
        }

        model.addAttribute("pagers", studentService.collectBySemester(grade, department, major, semester));

        List<Collect> collects = studentService.collectBySemesterAll(grade, department, major, semester);

        model.addAttribute("collects", collects);

        return "page/collectFourthSemester";
    }
}
